package com.eptok.weixin.util;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;

import net.sf.json.JSONObject;
import yspay.express.store.WxConstant;
import yspos.cpfs.express.entity.ExpUserinfo;
import yspos.cpfs.express.entity.ExpWebchatUserinfo;
public class WxCooikeManager{
	private static final Logger LOG = Logger.getLogger(WxCooikeManager.class);
    public static void setCookie(HttpServletResponse response,String name,String value,int maxAge){
        Cookie  cookie = new Cookie(name,value);
        cookie.setPath("/");
        cookie.setMaxAge(maxAge);
		/*String domain = GlobalCache.getSystemCache("domain");
		LOG.info("set domain="+domain);
		if (!StringUtil.isEmpty(domain))
			cookie.setDomain(domain);*/
        response.addCookie(cookie);
    }
    public static void clearCookie(String cookieName,HttpServletRequest request,HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        try{
            for(int i=0;i<cookies.length;i++){
                Cookie cookie = new Cookie(cookieName, null);   
                cookie.setMaxAge(0);   
                cookie.setPath("/");
                response.addCookie(cookie);   
            }
        }catch(Exception e){
        	LOG.debug(e);
        }
    }
	public static void removeCookie(String cookieName, HttpServletResponse response) {
		Cookie cookie = new Cookie(cookieName, null);
		cookie.setMaxAge(0);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	public static Cookie getCookie(String cookieName, HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		Cookie retCookie = null;
		if(null == cookies)
		{
			return null;
		}
		for(Cookie cookie : cookies) {
			if (cookie.getName().equals(cookieName)) {
				retCookie = cookie;
			}
		}
		return retCookie;
	}
	public static String getCookieValue(String cookieName, HttpServletRequest request) {
		Cookie cookie = getCookie(cookieName, request);
		if(null == cookie)
		{
			return null;
		}
		return cookie.getValue();
	}
	
	
	
	
	
	//放入cookie
		public static void setWxUser(HttpServletResponse response,ExpWebchatUserinfo wxUser)
		{
			JSONObject json = JSONObject.fromObject(wxUser);
			String base64Str = Base64.encodeBase64URLSafeString(json.toString().getBytes());
			WxCooikeManager.setCookie(response, WxConstant.USER_WX_SESSION, base64Str, 24*60*60);//单位 秒
		}
		
		//获取
		public static ExpWebchatUserinfo getWxUser(HttpServletRequest request)
		{
			String base64JsonStr = WxCooikeManager.getCookieValue(WxConstant.USER_WX_SESSION, request);
			if(null == base64JsonStr) {
				return null;
			}
			byte[] base64 = Base64.decodeBase64(base64JsonStr);
			String jsonStr = new String(base64);
			JSONObject json = JSONObject.fromObject(jsonStr);
			ExpWebchatUserinfo user = (ExpWebchatUserinfo)json.toBean(json,ExpWebchatUserinfo.class);
			return user;
		}
		
		public static void setUser(HttpServletResponse response,ExpUserinfo user)
		{
			JSONObject json = JSONObject.fromObject(user);
			String base64Str = Base64.encodeBase64URLSafeString(json.toString().getBytes());
			WxCooikeManager.setCookie(response, WxConstant.USER_SESSION, base64Str, 24*60*60);//单位 秒
		}
		
		public static ExpUserinfo getUser(HttpServletRequest request)
		{
			String base64JsonStr = WxCooikeManager.getCookieValue(WxConstant.USER_SESSION, request);
			if(null == base64JsonStr) {
				return null;
			}
			byte[] base64 = Base64.decodeBase64(base64JsonStr);
			JSONObject json = JSONObject.fromObject(new String(base64));
			ExpUserinfo user = (ExpUserinfo)json.toBean(json,ExpUserinfo.class);
			return user;
		}
	
}
